package xyz.xtt.exchange.controller;

import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import xyz.xtt.exchange.entity.Test;
import xyz.xtt.exchange.mapper.TestMapper;
import xyz.xtt.exchange.service.ITestService;
import xyz.xtt.exchange.utils.ApiResult;

@Slf4j
@Schema(name = "下单返回对象", description = "下单返回对象")
@RestController
@RequestMapping("/api/exchange")
public class TestController {
	/**
	 * 下单接口
	 */
	@Autowired
	private ITestService iTestService;
	@Autowired
	private TestMapper testMapper;

	@Operation(summary = "下单接口")
	@PostMapping("/test")
	public ApiResult<Boolean> order(@RequestParam("file") MultipartFile file, @RequestBody Test test1) {
		log.info("test1:" + JSONUtil.toJsonStr(test1));
		Test test = Test.builder().id(IdUtil.getSnowflakeNextId()).name("😂🙂😁🐶😄😄🐶😂😙😬🤔😬😁🙂😁😁🐶😆😆😊😬").build();
		boolean save = iTestService.save(test);
		return ApiResult.ok(save);
	}

	@Operation(summary = "查询更新")
	@PostMapping("/test/update")
	public ApiResult<Test> update(Long id) {
		Test save = iTestService.selectForUpdate(id);
		boolean updateTarget = iTestService.updateTarget(save.getId(), UUID.randomUUID().toString());
		return ApiResult.ok(save);
	}

	@Operation(summary = "加锁更新")
	@PostMapping("/test/lockUpdate")
	public ApiResult<Boolean> lockUpdate(Long id) {
		boolean updateTarget = iTestService.lockUpdate(id, UUID.randomUUID().toString());
		return ApiResult.ok(updateTarget);
	}
}
